#ifndef UTILS_H
#define UTILS_H 1

#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include "../../../libSDL/include/SDL_thread.h"
#include "../../../libSDL/include/SDL_mutex.h"
#include "../../../libSDL/include/SDL_timer.h"
#include "../../../libSDL/include/SDL_audio.h"

#define PRIV_FIRST (0x01<<0)
#define PRIV_LAST  (0x01<<1)

typedef struct PrivateData{
	int64_t pts;
	uint16_t seq;
	uint16_t flags;	 // 01'b: head 01'b:tail 00': data
}PrivateData;

#define PKT_RSV_HDR (sizeof(PrivateData))

typedef struct Packet {
    int64_t pts; /* presentation time stamp in stream units (set av_set_pts_info) */
    uint8_t *head;
    uint8_t *data;
    int   size;
//    int   stream_index;
//    int   flags;
//    int   duration;
    void  (*destruct)(struct Packet *);    
    ////
    int   data_size; // data size
    int   intra;
} Packet; 

typedef struct PacketList {
    Packet pkt;
    struct PacketList *next;
} PacketList;


// structrue to queue packet
typedef struct PacketQueue {
    PacketList *first_pkt, *last_pkt;
    int nb_packets;
    int size;
    int abort_request;
    SDL_mutex *mutex;
    SDL_cond *cond;
} PacketQueue;


#define ERROR_UNKNOWN     (-1)  /* unknown error */
#define ERROR_IO          (-2)  /* i/o error */
#define ERROR_NUMEXPECTED (-3)  /* number syntax expected in filename */
#define ERROR_INVALIDDATA (-4)  /* invalid data found */
#define ERROR_NOMEM       (-5)  /* not enough memory */
#define ERROR_NOFMT       (-6)  /* unknown format */


void init_packet(Packet *pkt);
int new_packet(Packet *pkt, int size);
void free_packet(Packet *pkt);
void packet_queue_init(PacketQueue *q);
void packet_queue_end(PacketQueue *q);
int packet_queue_put(PacketQueue *q, Packet *pkt);
void packet_queue_abort(PacketQueue *q);
int packet_queue_get(PacketQueue *q, Packet *pkt, int block);
void  packet_queue_flush(PacketQueue *q);

int64_t gettime(void);

#endif // UTILS_H
